<?php
if(!check_manager_login()) exit('无权查看，请登录!');
require_once(LIB_PATH.DS.'upload.class.php');
require_once(LIB_PATH.DS.'thumbnail.class.php');
require_once MODEL_PATH.DS.'Album.php';
$Album = new Album();

if(check_post()){
	$image_ids = $_POST['photo_id'];
	$old_image = explode(",",$_POST['oldimgids']);
	if(!$image_ids) $image_ids = array();
	$diff_ids = implode(",",array_diff($old_image, $image_ids));
	if($diff_ids){
		$del_img = $Album->getPhotoByIds($diff_ids);
		if($del_img){
			foreach($del_img as $del){
				if(file_exists(SHOP_IMAGES.$del['image'])){
					@unlink(SHOP_IMAGES.$del['image']);
				}
				if(file_exists(SHOP_IMAGES.$del['thumbnail'])){
					@unlink(SHOP_IMAGES.$del['thumbnail']);
				}
			}
		}
		$result = $Album->delPhotoByIds($diff_ids);
	}
	
	$img_url = $_POST['url'];
	$album_id = $_POST['album_id'];
	$name = $_POST['name'];
	$description = $_POST['description'];
	$Upload = new Upload('shop_photos/');
	$Upload->setMaxSize(1002400);
	$upload_status = $Upload->move();
	$messages = $Upload->getMessages();
	
	$image_arr = $Upload->getFilename();
	if($image_arr){
		foreach($image_arr as $k => $img){
			$Thumbnail = new Thumbnail(SHOP_IMAGES.$img);
			$Thumbnail->setDestination(SHOP_IMAGES);
			$Thumbnail->create();
			$thb_name = $Thumbnail->getFilename();
			$result = $Album->addPhoto($album_id,$name[$k],$description[$k],$img,$thb_name,$img_url[$k]);
		}
	}

	$result = $Album->updateAlbumImage($album_id,$thb_name);
	if($result){
		redirect('index.php?mod=album&act=list','上传相册图片成功！');
	}
}
$album_id = isset($_GET['album_id'])?(int)$_GET['album_id']:'';
$name_list = $Album->getAlbumList();
$album_select = create_select($name_list,'id','name',$album_id);

if($album_id){
	$photo_list = $Album->getPhotoListByAlbumId($album_id);
	$img_ids = get_array($photo_list,'id','string');
}
include load_tpl('album_upload');
?>